/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-11-27     yoyoyes      first version
 */

#include <thread_timeslice_demo.h>

/*
 * @brief  线程入口函数
 *
 * @param  parameter 线程入口函数参数
 *
 * @return None
 */
static void thread_entry(void *parameter)
{
    rt_uint32_t value;
    rt_uint32_t count = 0;

    value = (rt_uint32_t)parameter;

    while(1)
    {
        if(0 == (count % 5))
        {
            rt_kprintf("thread %d, thread %d count = %d\n", value, value, count);

            if(count > 100)
            {
                return;
            }
        }

        count++;
    }
}

/*
 * @brief  线程时间片轮转调度Demo函数
 *
 * @param  None
 *
 * @return None
 */
void thread_timeslice_demo(void)
{
    rt_thread_t tid = RT_NULL;

    /* 创建线程1 */
    tid = rt_thread_create("thread1", thread_entry, (void*)1, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
    if(tid != RT_NULL)
    {
        rt_thread_startup(tid);
    }

    /* 创建线程2 */
    tid = rt_thread_create("thread2", thread_entry, (void*)2, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE - 5);
    if(tid != RT_NULL)
    {
        rt_thread_startup(tid);
    }
}

